package com.hoperun.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hoperun.bean.AppFeedbacks;
import com.hoperun.controller.common.BaseController;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.models.DataView;
import com.hoperun.framework.query.Criteria;
import com.hoperun.service.IAppFeedbacksSV;
import com.hoperun.utils.Constant;
import com.hoperun.utils.ErrorCode;

/**
 * Copyright: Copyright (c) 2015 XHZ <br/>
 * ClassName: AppFeedbacksController.java
 * 
 * @Description:
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2014-7-23 上午9:11:06
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2014-7-23
 *        fuyu v1.0.0 修改理由
 */
@Controller
@RequestMapping("/admin/app/feedbacks")
@RequiresUser
public class AppFeedbacksController extends BaseController {

	@Resource
	private IAppFeedbacksSV appFeedbacksSV;

	@ModelAttribute
	public void populateModel(final HttpServletRequest request,
			ModelMap modelMap) {
		try {
			super.initMenus(request, modelMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/{menu}/{menuNext}")
	public String index(@PathVariable long menu, @PathVariable long menuNext,
			ModelMap modelMap) {

		modelMap.put("menuType", menu);
		modelMap.put("menuNextType", menuNext);

		return "admin:appfeedbacks.list";
	}

	@RequestMapping("/page")
	@ResponseBody
	public BootGridPage<AppFeedbacks> identityUserPage(
			BootGrid<AppFeedbacks> grid, AppFeedbacks appFeedbacks,
			ModelMap modelMap) {
		BootGridPage<AppFeedbacks> pageModel = new BootGridPage<AppFeedbacks>();

		try {
			Criteria criteria = new Criteria();

			if (null == grid)
				return new BootGridPage<AppFeedbacks>();

			pageModel = appFeedbacksSV.queryPage(criteria, grid);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageModel;
	}

	@RequestMapping("/create")
	public String create(ModelMap modelMap) {
		modelMap.addAttribute("method", "save");
		return "adminAlone:appfeedbacks.info";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AppFeedbacks appFeedbacks = null;
		try {
			appFeedbacks = appFeedbacksSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("method", "update");
		modelMap.addAttribute("row", appFeedbacks);
		return "adminAlone:appfeedbacks.info";
	}

	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable long id, ModelMap modelMap,
			final HttpServletRequest request) {
		AppFeedbacks appFeedbacks = null;
		try {
			appFeedbacks = appFeedbacksSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelMap.addAttribute("detail", appFeedbacks);
		return "adminAlone:appfeedbacks.detail";
	}

	@RequestMapping("/save")
	@ResponseBody
	public DataView save(AppFeedbacks appFeedbacks) {
		DataView dataView = new DataView();

		try {
			appFeedbacksSV.save(appFeedbacks);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("新增成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/update")
	@ResponseBody
	public DataView update(AppFeedbacks appFeedbacks) {
		DataView dataView = new DataView();

		if (null == appFeedbacks || Constant.ZERO >= appFeedbacks.getFdId()) {
			throw new XHZException(ErrorCode.MISS_PARAM, null,
					new Object[] { "id信息" });
		}

		try {
			appFeedbacksSV.update(appFeedbacks);

			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataView;
	}

	@RequestMapping("/find/{id}")
	@ResponseBody
	public AppFeedbacks findOne(@PathVariable long id) {
		AppFeedbacks appFeedbacks = null;
		try {
			appFeedbacks = appFeedbacksSV.queryById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return appFeedbacks;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public DataView delete(Long[] ids) {
		DataView dataView = new DataView();
		AppFeedbacks appFeedbacks = null;
		try {
			if (null == ids) {
				throw new XHZException(ErrorCode.ILLEGAL_PARAM, null,
						new Object[] { "id" });
			}

			for (Long id : ids) {
				appFeedbacks = new AppFeedbacks();
				appFeedbacks.setFdId(id);
				// appFeedbacksSV.update(appFeedbacks);

			}
			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dataView;
	}
}
